package com.dingtalk.dingbotmgr.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dingtalk.dingbotmgr.model.Company;
import com.dingtalk.dingbotmgr.model.Company.Type;
import com.dingtalk.dingbotmgr.model.Project;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * Created by wyx on 2017/2/28.
 */
@Component("projectAction")
@Scope("prototype")
public class ProjectAction extends BaseAction {

    private Project project;

    private List<Project> projectList;

    private Map<Type, List<Company>> companyMap;

    private Company company;

    public String listProject() {
        companyMap = new HashMap<>();
        for (Type type : Type.values()) {
            companyMap.put(type, companyService.listByType(type));
        }
        if (company == null || company.getId() == null) {
            projectList = projectService.list();
        } else {
            projectList = projectService.listByCompany(company);
        }
        return SUCCESS;
    }

    public String saveProject() {
        projectService.saveOrUpdate(project);
        return SUCCESS;
    }

    public String deleteProject() {
        project = projectService.getEntityById(project.getId());
        projectService.delete(project);
        return SUCCESS;
    }

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public List<Project> getProjectList() {
        return projectList;
    }

    public void setProjectList(List<Project> projectList) {
        this.projectList = projectList;
    }

    public Map<Type, List<Company>> getCompanyMap() {
        return companyMap;
    }

    public void setCompanyMap(
        Map<Type, List<Company>> companyMap) {
        this.companyMap = companyMap;
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }
}
